package com.zxu.test.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.Observable;
import androidx.lifecycle.ViewModelProviders;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.zxu.test.EventBusUtil.AppEventMsg;
import com.zxu.test.EventBusUtil.BroadcastEvent;
import com.zxu.test.RxUtil.RxBus;
import com.zxu.test.RxUtil.RxEventMsg;
import com.zxu.test.application.MyApplication;
import com.zxu.test.base.BaseFragment;
import com.zxu.test.studyapp.databinding.ActivityTryBinding;
import com.zxu.test.util.Event;
import com.zxu.test.view.ToastCustom;
import org.greenrobot.eventbus.EventBus;

public class TryFragment extends BaseFragment {
    private ActivityTryBinding mBinding;
    private TryViewModel mViewModel;
    //本地广播 快 外部app接收不到
    private LocalBroadcastManager lbm;
    public TryFragment() { }

    public static TryFragment getInstance() {
        return new TryFragment();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = ActivityTryBinding.inflate(inflater, container, false);
        mViewModel = ViewModelProviders.of(getActivity()).get(TryViewModel.class);
        mBinding.setViewModel(mViewModel);
        lbm = LocalBroadcastManager.getInstance(getContext());
        return  mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupListener();
    }

    private void setupListener() {
        mViewModel.getBroadcastCommand().observe(getViewLifecycleOwner(), this::goToSendBrodcast);
        mViewModel.btnEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                ToastCustom.getInstance(MyApplication.getInstance()).show(mViewModel.btnEvent.get(),1000);
                EventBus.getDefault().post(new AppEventMsg(AppEventMsg.SPLASH_FRAGMENT_MSG));
            }
        });
        mViewModel.rxEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                RxBus.getInstance().post(new RxEventMsg(RxEventMsg.RXBUS_TRY_MSG));
            }
        });
        mViewModel.cancelEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                ToastCustom.getInstance(MyApplication.getInstance()).show("取消",1000);
            }
        });
        mViewModel.saveEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
            }
        });
    }

    private void goToSendBrodcast(Event<Object> taskIdEvent) {
        String str = taskIdEvent.getContentIfNotHandled().toString();
        ToastCustom.getInstance(MyApplication.getInstance()).show(str,1000);
        lbm.sendBroadcast(new Intent(BroadcastEvent.SEND_LOCALBROADCAST_MSG));
    }
}
